<?php

namespace app\api\controller;

use app\services\AreaService;
use app\services\ArticleService;
use app\services\ConfigService;
use app\services\DictService;
use app\services\ExchangeRateService;
use app\services\PaymentService;
use app\services\QuestionService;
use app\services\SmsService;
use app\services\VersionService;
use app\services\VideoService;
use plugin\admin\app\model\Base;
use plugin\admin\app\model\ExpressCompany;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserProfile;
use support\FileStore;
use support\Request;
use support\Store;

class CommonController
{
    public function siteName()
    {
        $site = ConfigService::getByGroup('site');
        return ok_rsp('成功', ['title' => $site['site_name'] ?? '', 'logo' => $site['site_logo'] ?? '/app/admin/admin/images/logo.png']);
    }

    public function allKeys(Request $request)
    {
        return ok_rsp('成功', DictService::getAll());
    }

    public function coinConfig(Request $request)
    {
        return ok_rsp('成功', ExchangeRateService::data());
    }

    public function config(Request $request)
    {
        $integralToBalance = ConfigService::get('integral_to_balance');

        return ok_rsp('成功', ['integral_to_balance' => $integralToBalance]);
    }

    public function giftConfig(Request $request)
    {
        $config = ConfigService::getByGroup('rebate');
        return ok_rsp('成功', ['integral' => $config['rebate_integral'], 'contribution' => $config['rebate_contribution']]);
    }

    public function upload(Request $request)
    {
        $url = Store::autoGetInstance()->upload($request);

        return ok_rsp('上传成功', ['url' => $url]);
    }

    public function customerServiceInfo(Request $request)
    {
        try {
            $data = ConfigService::get('customer_service');

            $data['customer_service_avatar'] = full_url($data['customer_service_avatar']);
            $data['customer_service_qrcode'] = full_url($data['customer_service_qrcode']);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function checkVersion(Request $request)
    {
        try {
            $data = VersionService::checkVersion($request->post('type'), $request->post('current_version'));
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }


    public function siteInfo(Request $request)
    {
        try {
            $data = ConfigService::getByGroup('site');

            $data['site_logo'] = full_url($data['site_logo']);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function areas(Request $request)
    {
        $parentId = $request->post('parent_id', 0);

        try {
            $data = AreaService::getAreas($parentId);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function cities(Request $request)
    {
        try {
            $data = AreaService::cities();
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function sendSms(Request $request)
    {
        try {
            $mobile = $request->post('mobile');
            $scene = $request->post('scene');
            $user = User::where('id', get_user_id())->first();

            if (!$mobile) {
                $mobile = $user['mobile'];

                if (!$mobile) {
                    throw new \Exception('请输入手机号码');
                }
            }

            SmsService::send($mobile, $scene);
            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }

    public function protocol(Request $request)
    {
        try {
            $key = $request->post('key');
            $language = $request->post('language', 'zh');
            $data = ArticleService::detail($key, $language);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function kefu(Request $request)
    {
        try {
            $config = ConfigService::getByGroup('kefu');
            $config['kefu_qrcode'] = full_url($config['kefu_qrcode']);
            return ok_rsp('成功', $config);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function expressList(Request $request)
    {
        try {
            return ok_rsp('成功', ExpressCompany::all()->toArray());
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function languages(Request $request)
    {
        try {
            $languages = Base::LANGUAGE_TEXT;

            $data = [];

            foreach ($languages as $key=>$value) {
                $data[] = [
                    'key' => $key,
                    'value' => $value
                ];
            }


            return ok_rsp('成功', ['languages' => $data]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function question(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('page', 10);
            $categoryId = $request->post('category_id');

            $data = QuestionService::list($page, $length, $categoryId);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function priceConfig(Request $request)
    {
        try {
            $data = ConfigService::priceConfig();
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function touristConfig(Request $request)
    {
        try {
            $data = ConfigService::touristConfig();
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function withdrawConfig(Request $request)
    {
        try {
            $config = ConfigService::get('site');

            return ok_rsp('成功', ['min_withdraw_amount' => $config['min_withdraw_amount'], 'withdraw_fee' => $config['withdraw_fee']]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }

    public function addSurnameConfig(Request $request)
    {
        try {

            return ok_rsp('成功', ConfigService::addSurnameConfig());
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }

    public function getLiveBroadcastUrl(Request $request)
    {
        try {
            $data = ConfigService::get('logo');

            return ok_rsp('成功', ['url' => $data['live_broadcast_address']]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getCommonAddress(Request $request)
    {
        try {
            $data = ConfigService::get('logo');

            return ok_rsp('成功', ['address' => $data['common_address']]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}